﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public partial class UserControlLyLichSoLuoc : UserControl
    {
        private LyLichSoLuoc lyLichSoLuoc;
        private ThongTinPhu thongTinPhu;
        private NgheNghiep ngheNghiep;

        private bool isEveryThingOK = false;
        private Database myDatabase = Database.shareDatabase;


        // bool for textbox
        bool isHotenOK = false;
        bool isPhaiOK = true;
        bool isNoiSinhOK = false;
        bool isChucVuOK = false;
        bool isBiDanhOK = false;
        bool isChuyenMonOK = false;
        bool isDonViCongTacOK = false;
        bool isLuongOK = false;

        public UserControlLyLichSoLuoc()
        {
            InitializeComponent();

            // Khởi tạo ngôn ngữ cho toàn bộ label
            this.initLanguage();

            // Ẩn toàn bộ pictureBox
            this.hiddenAllErrorImage();

            // Khởi Tạo combobox
            this.initConboBox();
        }

        private void initConboBox()
        {
            // load default values
            DataTable tablePhai = new DataTable();
            tablePhai.Columns.Add("Phai", typeof(string));
            tablePhai.Columns.Add("ID", typeof(int));
            tablePhai.Rows.Add(Language._("PHAI_NAM_LBL"),0);
            tablePhai.Rows.Add(Language._("PHAI_NU_LBL"), 1);

            cbxPhai.DataSource = tablePhai;
            cbxPhai.DisplayMember = "Phai";
            cbxPhai.ValueMember = "ID";
            cbxPhai.SelectedIndex = 0;
        }

        private void initLanguage()
        {
            lblHoTen.Text = Language._("LYLICHSOLUOC_FORM_HOTEN_LBL");
            lblNgaySinh.Text = Language._("LYLICHSOLUOC_FORM_NGAYSINH_LBL");
            lblNoiSinh.Text = Language._("LYLICHSOLUOC_FORM_NOISINH_LBL");
            lblBiDanh.Text = Language._("LYLICHSOLUOC_FORM_BIDANH_LBL");
            lblChucVu.Text = Language._("LYLICHSOLUOC_FORM_CHUCVU_LBL");
            lblChuyenMon.Text = Language._("LYLICHSOLUOC_FORM_CHUYENMON_LBL");
            lblDonVICongTac.Text = Language._("LYLICHSOLUOC_FORM_DONVICONGTAC_LBL");
            lblLuong.Text = Language._("LYLICHSOLUOC_FORM_LUONG_LBL");
            lblPhai.Text = Language._("LYLICHSOLUOC_FORM_PHAI_LBL");

            
            lblNguyenQuan.Text = Language._("LYLICHSOLUOC_FORM_NGUYENQUAN_LBL");
            lblDiaChi.Text = Language._("LYLICHSOLUOC_FORM_DIACHI_LBL");
            lblThanhPhan.Text = Language._("LYLICHSOLUOC_FORM_THANHPHAN_LBL");
            lblChucVuCaoNhat.Text = Language._("LYLICHSOLUOC_FORM_CHUCVUCAONHAT_LBL");
            lblNoiCVCN.Text = Language._("LYLICHSOLUOC_FORM_NOICVCN_LBL");
            lblTinhTrangSucKhoe.Text = Language._("LYLICHSOLUOC_FORM_TINHTRANGSUCKHOE_LBL");
            lblDanToc.Text = Language._("LYLICHSOLUOC_FORM_DANTOC_LBL");
            lblTonGiao.Text = Language._("LYLICHSOLUOC_FORM_TONGIAO_LBL");
            lblNgayVaoDoan.Text = Language._("LYLICHSOLUOC_FORM_NGAYVAODOAN_LBL");
            lblNgayVaoDang.Text = Language._("LYLICHSOLUOC_FORM_NGAYVAODANG_LBL");
        }

        /// <summary>
        /// ẩN toàn bộ pic
        /// </summary>
        private void hiddenAllErrorImage()
        {
            pictureBoxBiDanh.Visible = false;
            pictureBoxChucVu.Visible = false;
            pictureBoxChuyenMon.Visible = false;
            pictureBoxDonviCongTac.Visible = false;
            pictureBoxHoTen.Visible = false;
            pictureBoxLuong.Visible = false;
            pictureBoxNoiSInh.Visible = false;
            pictureBoxNoiSInh.Visible = false;
        }

        private void txbHoTen_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            // check Ho Ten == empty
            if (txbHoTen.Text.Trim() == "")
                isHasError = true;
            /*
        else
        // check Ho Ten is duplicated
        if (Database.isDupplicate("LyLichSoLuoc","HoTen",txbHoTen.Text))
            isHasError = true;
        */
            if (isHasError)
            {
                isHotenOK = false;
                
                // change Hoten's background color to Tomato
                // Add UserTip
                txbHoTen.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_HOTEN_NULL");
                utpTip.Visible = true;
                pictureBoxHoTen.Visible = false;
            }
            else
            {
                isHotenOK = true;
                txbHoTen.BackColor = Color.White;
                pictureBoxHoTen.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbNoiSinh_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbNoiSinh.Text.Trim() == "")
                isHasError = true;
            
            if (isHasError)
            {
                isNoiSinhOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbNoiSinh.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_NOISINH_NULL");
                utpTip.Visible = true;
                pictureBoxNoiSInh.Visible = false;
            }
            else
            {
                isNoiSinhOK = true;
                txbNoiSinh.BackColor = Color.White;
                pictureBoxNoiSInh.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbBiDanh_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbBiDanh.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isBiDanhOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbBiDanh.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_BIDANH_NULL");
                utpTip.Visible = true;
                pictureBoxBiDanh.Visible = false;
            }
            else
            {
                isBiDanhOK = true;
                txbBiDanh.BackColor = Color.White;
                pictureBoxBiDanh.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbChucVu_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbChucVu.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isChucVuOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbChucVu.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_CHUCVU_NULL");
                utpTip.Visible = true;
                pictureBoxChucVu.Visible = false;
            }
            else
            {
                isChucVuOK = true;
                txbChucVu.BackColor = Color.White;
                pictureBoxChucVu.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbChuyenMon_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbChuyenMon.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isChuyenMonOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbChuyenMon.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_CHUYENMON_NULL");
                utpTip.Visible = true;
                pictureBoxChuyenMon.Visible = false;
            }
            else
            {
                isChuyenMonOK = true;
                txbChuyenMon.BackColor = Color.White;
                pictureBoxChuyenMon.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbDonViCongTac_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbDonViCongTac.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isDonViCongTacOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbDonViCongTac.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_DONVICONGTAC_NULL");
                utpTip.Visible = true;
                pictureBoxDonviCongTac.Visible = false;
            }
            else
            {
                isDonViCongTacOK = true;
                txbDonViCongTac.BackColor = Color.White;
                pictureBoxDonviCongTac.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbLuong_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float outResult;

            // check Luong is numberic
            if (!float.TryParse(txbLuong.Text, out outResult))
            {
                isHasError = true;
            }

            if (isHasError)
            {
                isDonViCongTacOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbLuong.BackColor = Color.Tomato;
                utpTip.Value = Language._("LyLichSoLuocFORM_TIP_LUONG_NULL");
                utpTip.Visible = true;
                pictureBoxLuong.Visible = false;
            }
            else
            {
                isLuongOK = true;
                txbLuong.BackColor = Color.White;
                pictureBoxLuong.Visible = true;
                utpTip.Visible = false;
            }
        }

        public bool isLyLichSoLuocOK()
        {
            // Validate
            txbHoTen_Validating(txbHoTen, null);
            txbNoiSinh_Validating(txbNoiCVCN, null);
            txbBiDanh_Validating(txbBiDanh, null);
            txbChucVu_Validating(txbChucVu, null);
            txbChuyenMon_Validating(txbChuyenMon, null);
            txbDonViCongTac_Validating(txbDonViCongTac, null);
            txbLuong_Validating(txbLuong, null);

            // check every textbox is ok
            if (isHotenOK && isPhaiOK && isNoiSinhOK && isBiDanhOK && isChuyenMonOK && isChucVuOK && isDonViCongTacOK && isLuongOK)
                isEveryThingOK = true;
            else
                isEveryThingOK = false;

            // return
            if (isEveryThingOK)
                return true;
            else
                return false;
        }

        public LyLichSoLuoc getLyLichSoLuoc()
        {
            if (isEveryThingOK)
            {
                ngheNghiep = new NgheNghiep(txbDonViCongTac.Text, txbChucVu.Text, txbChuyenMon.Text, float.Parse(txbLuong.Text));
                thongTinPhu = new ThongTinPhu(txbNguyenQuan.Text, txbDanToc.Text, txbDiaChi.Text, txbTonGiao.Text, txbThanhPhan.Text, dtpNgayVaoDoan.Value, dtpNgayVaoDang.Value, txbChucVu.Text, txbNoiCVCN.Text, dtpThoiGianCVCN.Value, txbTinhTrangSucKhoe.Text);
                lyLichSoLuoc = new LyLichSoLuoc(txbHoTen.Text, dtpNgaySinh.Value, txbNoiSinh.Text, Int32.Parse(cbxPhai.SelectedValue.ToString()), txbBiDanh.Text, thongTinPhu, ngheNghiep);

            }
            return lyLichSoLuoc;
        }
    }
}
